package com.mocha.common.core;

import lombok.*;

import java.io.Serializable;


/**
 * @author: songkui
 * @date: 2022/3/15 14:56
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class MochaResponse<T> implements Serializable {

    private int state;
    private int opcode;
    private int wscmd; // websocket 命令号
    private String message;
    private T data;



    public static <T> MochaResponse<T> response(T data, ResponseCode responseCode){
        return MochaResponse.builder().response(data, responseCode).build();
    }

    public static  <T>  MochaResponse<T> responseCode(ResponseCode code){
        return MochaResponse.builder().state(code.code()).opcode(code.opcode()).message(code.msg()).build();
    }
    public static <T> MochaResponse<T> responseCode(ResponseCode code, int wscmd){
        return MochaResponse.builder().wscmd(wscmd).state(code.code()).opcode(code.opcode()).message(code.msg()).build();
    }

    public static <T> MochaResponse<T> responseSuccess(T data){
        return MochaResponse.builder().success(data).build();
    }

    public boolean isSuccess(){
        return this.state == ResponseCode.Success.code() ? true : false;
    }



    public static   MochaResponseBuilder builder() {
        return new MochaResponse.MochaResponseBuilder<>();
    }

    public static class MochaResponseBuilder<T> {
        private int state;
        private int opcode;
        private int wscmd;
        private String message;
        private T data;

        MochaResponseBuilder() {
        }


        public MochaResponse.MochaResponseBuilder<T> state(int state) {
            this.state = state;
            return this;
        }

        public MochaResponse.MochaResponseBuilder<T> opcode(int opcode) {
            this.opcode = opcode;
            return this;
        }

        public MochaResponse.MochaResponseBuilder<T> wscmd(int wscmd) {
            this.wscmd = wscmd;
            return this;
        }

        public MochaResponse.MochaResponseBuilder<T> message(String message) {
            this.message = message;
            return this;
        }

        public MochaResponse.MochaResponseBuilder<T> data(T data) {
            this.data = data;
            return this;
        }


        public MochaResponse.MochaResponseBuilder<T> response(T data, ResponseCode code) {
            this.state = code.code();
            this.message = code.msg();
            this.opcode = code.opcode();
            this.data = data;
            return this;
        }

        public MochaResponse.MochaResponseBuilder<T> success(T data) {
            return response(data, ResponseCode.Success);
        }



        public MochaResponse<T> build() {
            return new MochaResponse<>(this.state, this.opcode, this.wscmd, this.message, this.data);
        }

    }


}
